package com.blue.common.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

public class BusinessUtils {
    /**
     * 自定义分页
     *
     * @param pageNo
     * @param pageSize
     * @param list
     * @return
     */
    public static IPage getPage(Integer pageNo, Integer pageSize, List list) {
        IPage<Object> page = new Page<>();
        if (list != null && list.size() > 0) {
            int size = list.size();
            if (pageSize > size) {
                pageSize = size;
            }
            int maxPage = size % pageSize == 0 ? size / pageSize : size / pageSize + 1;
            if (pageNo > maxPage) {
                pageNo = maxPage;
            }
            int index = pageNo > 1 ? (pageNo - 1) * pageSize : 0;
            List<Object> objs = new ArrayList<>();
            for (int i = 0; i < pageSize && index + i < size; i++) {
                objs.add(list.get(index + i));
            }
            page.setRecords(objs).setTotal(list.size());
        }
        page.setCurrent(pageNo).setSize(pageSize);
        return page;
    }

    /**
     * 判断对象字段是否有值为null的字段
     *
     * @param object
     * @param invFields
     * @return
     */
    public static boolean isFieldNull(Object object, List<String> invFields) {
        boolean flag = true;
        Class<?> clazz = object.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Object value = null;
            try {
                if (!invFields.contains(field.getName())) {
                    value = field.get(object);
                    if (value == null) {
                        flag = false;
                        break;
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }

    /**
     * 拆分集合
     *
     * @param list
     * @param size
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> splitCollection(List<T> list, int size) {
        List<List<T>> chunks = new ArrayList<>();
        final int listSize = list.size();
        IntStream.range(0, (listSize + size - 1) / size)
                .mapToObj(i -> list.subList(i * size, Math.min(listSize, (i + 1) * size)))
                .forEach(chunks::add);
        return chunks;
    }

    /**
     * 正则匹配字符串
     *
     * @param pattern
     * @param content
     * @return
     */
    public static List<String> regularMatch(String pattern, String content) {
        Pattern p = Pattern.compile(pattern);
        Matcher matcher = p.matcher(content);
        List<String> matched = new ArrayList<>();
        while (matcher.find()) {
            matched.add(matcher.group());
        }
        return matched;
    }

    public static String getClientIP(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
